package edu.westga.movierec.GUI;
import java.awt.Component;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButton;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.SpinnerListModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingConstants;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;

import edu.westga.movierec.controllers.*;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class RecGUI extends javax.swing.JFrame {
	private JLabel UserLabel;
	private JSpinner UserSpinner;
	private JRadioButton yesButton;
	private JButton ratingButton;
	private AbstractAction resetUserMenu;
	private JMenuItem jMenuItem1;
	private JMenu jMenu1;
	private JMenuBar MenuBar;
	private AbstractAction submitAction;
	private AbstractAction RecommendAction;
	private ButtonGroup PrefButtons;
	private JRadioButton noButton;
	private JLabel PreferenceLabel;
	private JTextField RecField;
	private JButton RecButton;
	private JTextField movieText;
	private JLabel MovieLabel;
	private UserController theUserController;

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				RecGUI inst = new RecGUI();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public RecGUI() {
		super();
		theUserController = new UserController();
		initGUI();
	}
	
	private void initGUI() {
		try {
			GroupLayout thisLayout = new GroupLayout((JComponent)getContentPane());
			getContentPane().setLayout(thisLayout);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			this.setTitle("Movie Recommender");
			{
				MenuBar = new JMenuBar();
				setJMenuBar(MenuBar);
				MenuBar.add(getJMenu1());
			}
			{
				UserLabel = new JLabel();
				UserLabel.setText("User:");
				UserLabel.setFont(new java.awt.Font("Segoe UI",0,18));
			}
			{
				SpinnerNumberModel UserSpinnerModel =
					new SpinnerNumberModel(1,0,1000,1);
				UserSpinner = new JSpinner();
				UserSpinner.setModel(UserSpinnerModel);
			}
			{
				MovieLabel = new JLabel();
				MovieLabel.setText("Movie:");
				MovieLabel.setFont(new java.awt.Font("Segoe UI",0,18));
			}
			{
				movieText = new JTextField();
			}
			{
				RecButton = new JButton();
				RecButton.setText("Recommend");
				RecButton.setAction(getRecommendAction());
			}
			{
				RecField = new JTextField();
				RecField.setFont(new java.awt.Font("Segoe UI",1,12));
				RecField.setEditable(false);
			}
			{
				PreferenceLabel = new JLabel();
				PreferenceLabel.setText("Do you like this movie?");
				PreferenceLabel.setEnabled(false);
			}
			{
				yesButton = new JRadioButton();
				yesButton.setText("Thumbs Up");
				yesButton.setEnabled(false);
				getPrefButtons().add(yesButton);
			}
			{
				noButton = new JRadioButton();
				noButton.setText("Thumbs Down");
				noButton.setEnabled(false);
				getPrefButtons().add(noButton);
				getRatingButton().setEnabled(false);
			}
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				    .addComponent(MovieLabel, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
				    .addComponent(UserLabel, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				    .addComponent(movieText, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE)
				    .addComponent(UserSpinner, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE))
				.addGap(22)
				.addComponent(RecButton, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				.addGap(17)
				.addComponent(RecField, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				.addComponent(PreferenceLabel, GroupLayout.PREFERRED_SIZE, 19, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				    .addComponent(yesButton, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .addComponent(noButton, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 18, GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				.addComponent(getRatingButton(), GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 0, Short.MAX_VALUE));
			thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addGroup(thisLayout.createParallelGroup()
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addComponent(UserSpinner, GroupLayout.PREFERRED_SIZE, 84, GroupLayout.PREFERRED_SIZE)
				        .addGap(46)
				        .addGroup(thisLayout.createParallelGroup()
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(MovieLabel, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE)
				                .addGap(0, 29, Short.MAX_VALUE))
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addGap(0, 8, Short.MAX_VALUE)
				                .addComponent(noButton, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))))
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addComponent(UserLabel, GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE)
				        .addComponent(getRatingButton(), GroupLayout.PREFERRED_SIZE, 126, GroupLayout.PREFERRED_SIZE)
				        .addGap(0, 46, Short.MAX_VALUE))
				    .addComponent(RecField, GroupLayout.Alignment.LEADING, 0, 234, Short.MAX_VALUE)
				    .addGroup(thisLayout.createSequentialGroup()
				        .addPreferredGap(UserSpinner, yesButton, LayoutStyle.ComponentPlacement.INDENT)
				        .addGroup(thisLayout.createParallelGroup()
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(yesButton, GroupLayout.PREFERRED_SIZE, 96, GroupLayout.PREFERRED_SIZE)
				                .addGap(0, 15, Short.MAX_VALUE)
				                .addComponent(movieText, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE))
				            .addGroup(thisLayout.createSequentialGroup()
				                .addGap(32)
				                .addGroup(thisLayout.createParallelGroup()
				                    .addGroup(thisLayout.createSequentialGroup()
				                        .addComponent(RecButton, GroupLayout.PREFERRED_SIZE, 144, GroupLayout.PREFERRED_SIZE))
				                    .addGroup(thisLayout.createSequentialGroup()
				                        .addComponent(PreferenceLabel, GroupLayout.PREFERRED_SIZE, 145, GroupLayout.PREFERRED_SIZE)))
				                .addGap(0, 34, Short.MAX_VALUE)))
				        .addGap(11)))
				.addContainerGap());
			thisLayout.linkSize(SwingConstants.VERTICAL, new Component[] {UserSpinner, movieText});
			pack();
			this.setSize(274, 300);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private JRadioButton getYesButton() {
		return yesButton;
	}
	
	private ButtonGroup getPrefButtons() {
		if(PrefButtons == null) {
			PrefButtons = new ButtonGroup();
		}
		
		return PrefButtons;
	}
	
	private JButton getRatingButton() {
		if(ratingButton == null) {
			ratingButton = new JButton();
			ratingButton.setText("Submit Rating");
			ratingButton.setEnabled(false);
			ratingButton.setAction(getSubmitAction());
		}
		return ratingButton;
	}
	
	private AbstractAction getRecommendAction() {
		if(RecommendAction == null) {
			RecommendAction = new AbstractAction("Recommend", null) {
				public void actionPerformed(ActionEvent evt) {
					int user = Integer.parseInt(UserSpinner.getValue().toString());
					String movie = movieText.getText();
					String rec = theUserController.getRecommendedMovie(user,movie);
					RecField.setText(rec);
					if(rec=="Invalid Movie"||rec=="Invalid User"
						||rec=="All movies are recommended."
						||rec=="Database not found."){
							getYesButton().setSelected(false);
							noButton.setSelected(false);
							PreferenceLabel.setEnabled(false);
							getYesButton().setEnabled(false);
							noButton.setEnabled(false);
							getRatingButton().setEnabled(false);
							return;
					}
					PreferenceLabel.setEnabled(true);
					getYesButton().setEnabled(true);
					noButton.setEnabled(true);
					getRatingButton().setEnabled(true);
				}
			};
		}
		return RecommendAction;
	}
	
	private AbstractAction getSubmitAction() {
		if(submitAction == null) {
			submitAction = new AbstractAction("Submit Rating", null) {
				public void actionPerformed(ActionEvent evt) {
					String movie = movieText.getText();
					String rec = RecField.getText();
					if(rec=="Invalid Movie"||rec=="Invalid User"
						||rec=="All movies are recommended."
						||rec=="Database not found."){
							getYesButton().setSelected(false);
							noButton.setSelected(false);
							PreferenceLabel.setEnabled(false);
							getYesButton().setEnabled(false);
							noButton.setEnabled(false);
							getRatingButton().setEnabled(false);
							return;
					}
					if(getYesButton().isSelected()){
						theUserController.responseToRec(movie,rec,true);
						getYesButton().setSelected(false);
						PreferenceLabel.setEnabled(false);
						getYesButton().setEnabled(false);
						noButton.setEnabled(false);
						getRatingButton().setEnabled(false);
					}
					if(noButton.isSelected()){
						theUserController.responseToRec(movie,rec,false);
						noButton.setSelected(false);
						PreferenceLabel.setEnabled(false);
						getYesButton().setEnabled(false);
						noButton.setEnabled(false);
						getRatingButton().setEnabled(false);
					}
				}
			};
		}
		return submitAction;
	}
	
	private JMenu getJMenu1() {
		if(jMenu1 == null) {
			jMenu1 = new JMenu();
			jMenu1.setText("File");
			jMenu1.add(getJMenuItem1());
		}
		return jMenu1;
	}
	
	private JMenuItem getJMenuItem1() {
		if(jMenuItem1 == null) {
			jMenuItem1 = new JMenuItem();
			jMenuItem1.setText("jMenuItem1");
			jMenuItem1.setAction(getResetUserMenu());
		}
		return jMenuItem1;
	}
	
	private AbstractAction getResetUserMenu() {
		if(resetUserMenu == null) {
			resetUserMenu = new AbstractAction("Reset User", null) {
				public void actionPerformed(ActionEvent evt) {
					int user = Integer.parseInt(UserSpinner.getValue().toString());
					theUserController.resetUser(user);
					getYesButton().setSelected(false);
					noButton.setSelected(false);
					RecField.setText("");
					PreferenceLabel.setEnabled(false);
					getYesButton().setEnabled(false);
					noButton.setEnabled(false);
					getRatingButton().setEnabled(false);
				}
			};
		}
		return resetUserMenu;
	}

}
